

function get_content(url_read, url_write, domain_used, biov_domain, id){
	var q  = '{"query":[{'+
		'"name":null,'+
		'"type":"'+biov_domain+'bv_medical_condition",'+
		'"company_involved" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/includes_diseases" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/parent_disease" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/risk_factors" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/stages" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/survival_rates" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/symptoms" : [{"*":null,"optional":true}],'+
		'"/medicine/disease/treatments" : [{"*":null,"optional":true}],'+
		'"guid":null,'+
		'"id" :"'+id+'",'+
		'"guid":null'+
		'}]}';
	
	perform_freebase_read(
		url_read,
		q,
		function (msg) {
			var disease = null;
			disease = msg.result[0];
            if (!disease) return;
			
			var diseaseInfo = Object();
			
			// Name
			var name = disease.name;
			$('#title').text(name);
			
			// Get the IDs
			diseaseInfo.guid = disease.guid;
			diseaseInfo.id = disease.id;
			// Get the overview text
			var topic_domain = biov_domain+'bv_medical_condition';
			
			jsonp_totalblurb_freebase(domain_used, name, topic_domain, 500,
					function(m){
						$("#thumb").attr('src','http://'+domain_used+'/api/trans/image_thumb'+m.result[0].image.id);
					},
					function(m){
						$("#description").append($('<span>'+m.result.body+'</span>'));
					},
					function(m){
						var test = 0;
					});
					
			// Get the parental-diseases
			var parentDiseases = Array();
			parentDiseases = disease['/medicine/disease/parent_disease'];
			var linkInfo = Object();
			linkInfo.useLink = true;
			linkInfo.relDir = 'disease';
			createListWithHeader(parentDiseases,'Parental Diseases','parentDiseases', 3, linkInfo, false, 50);
			diseaseInfo.parentDiseases = parentDiseases;
						
			// Symptoms
			var symptoms = Array();
			symptoms = disease['/medicine/disease/symptoms'];
			var linkInfo = Object();
			linkInfo.useLink = false;			
			createListWithHeader(symptoms,'Symptoms','symptoms',3,linkInfo,false,-1);
			diseaseInfo.symptoms = symptoms;

			// Treatments
			var treatments = Array();
			treatments = disease['/medicine/disease/treatments'];
			createListWithHeader(treatments,'Treatments','treatments',3,linkInfo,false,-1);
			diseaseInfo.treatments = treatments;

			// Stages
			var stages = Array();
			stages = disease['/medicine/disease/stages'];
			createListWithHeader(stages,'Stages','stages',3,linkInfo,false,-1);
			diseaseInfo.stages = stages;
			
			// Survival Rates
			var survivalStages = Array();
			survivalStages = disease['/medicine/disease/survival_rates'];
			if(survivalStages.length > 0){
			    var survH = '<h3><span>Survival Rates</span></h3>';
			    survH =$(survH);
			    $("#survivalH").append(survH);
			    
			    var survival = new google.visualization.DataTable();
				survival.addColumn('number', 'Years');
				survival.addColumn('number', 'Survival Rate (%)');
				survival.addColumn('string', 'Stage');
			 	survival.addRows(survivalStages.length);
			 	
			 	for (var j in survivalStages){
			 		var survData = survivalStages[j];
			 		
			 		survival.setCell(parseFloat(j), 0, survData.years);
			 		survival.setCell(parseFloat(j), 1, survData.rate);
			 		survival.setCell(parseFloat(j), 2, survData.disease_stage);
			 	}
			 	
			 	var survivalTable = new google.visualization.Table(document.getElementById('survivalTable'));
				survivalTable.draw(survival, {allowHtml: true, sortColumn: 2});		
			}
			
			// Get the sub-diseases		
			var subDiseases = Array();
			subDiseases = disease['/medicine/disease/includes_diseases'];
			var linkInfo = Object();
			linkInfo.useLink = true;
			linkInfo.relDir = 'disease';
			createListWithHeader(subDiseases,'Includes Diseases','subDiseases', 3, linkInfo, false, 50);
			diseaseInfo.subDiseases = subDiseases;
			
			
			// Read the therapeutics for all sub-diseases:
			// Some reason there's a bug when too many diseases are used.
			var numDisplayPerTime = 60;
			var numBlocks = Math.ceil(subDiseases.length / numDisplayPerTime);
			numBlocks = (numBlocks >0 ? numBlocks : 1);
			var diseaseBlocks = Array();
			for(var k=0;k<numBlocks;k++){
				var allDiseases = (k==0 ? '"'+name+'"' : '');
				var numDisplay = ((k+1)*numDisplayPerTime < subDiseases.length ? numDisplayPerTime : (numDisplayPerTime - ((k+1)*numDisplayPerTime - subDiseases.length)));
				for(var j=0; j<numDisplay; j++){
					allDiseases += ( j < numDisplay && !(k>0 && j==0) ? ', ' :"");
					var dis = subDiseases[(k*numDisplayPerTime + j)].name;
					allDiseases += '"'+dis+'"';
				}
				diseaseBlocks[k]=allDiseases;
			}
			
			//therapeutics
			read_therapeutics(diseaseBlocks, url_read, domain_used, biov_domain, name)
			
			//companies involved
			var companies = disease.company_involved;
			read_companies(companies, biov_domain, domain_used);
			var companyNames = Array();
			for(var k=0;k<companyNames.length;k++){
				companyNames[k] = companies.name;
			}
			diseaseInfo.companies = disease.company_involved;
			
			// Get the completion score for the disease:
			var completion_score = calculateDiseaseCompletion(diseaseInfo);
			var completion_string = '<input type="submit" id="completion_button" value="'+disease.name+' is only '+completion_score+'% complete. Create an Account to Add More Content!"></input>';		
			var completion_string_login = '<div>'+disease.name+' is  '+completion_score+'% complete. </div>';		
			$("#completion_score").append(completion_string);
			$("#completion_button").bind("click", function(e){
	        	//location.href = "{% url registration.views.register %}";
				location.href = '/account/register'
			})
			$("#completion_score_login").append(completion_string_login);			
			
			// Create the form for editing
			create_edit_form(url_read, url_write, domain_used, biov_domain, diseaseInfo, name);
					
		},
		function(m){
			var test = 0;
		}
	);	
}

function calculateDiseaseCompletion(diseaseInfo){
	var score = 0;
	
	if(typeof(diseaseInfo.parentDiseases) != 'undefined') {
		if(diseaseInfo.parentDiseases.length>0){
			score += 15;
		}
	}
	if(typeof(diseaseInfo.subDiseases) != 'undefined'){
		if(diseaseInfo.subDiseases.length>0){
			score += 15;
		}
	}
	if(typeof(diseaseInfo.symptoms) != 'undefined'){
		if(diseaseInfo.symptoms.length>0){
			score += 15;
		}
	}
	if(typeof(diseaseInfo.treatments) != 'undefined'){
		if(diseaseInfo.treatments.length>0){
			score += 15;
		}
	}
	if(typeof(diseaseInfo.subDiseases) != 'undefined'){
		if(diseaseInfo.subDiseases.length>0){
			score += 15;
		}
	}
	if(typeof(diseaseInfo.companies) != 'undefined'){
		if(diseaseInfo.companies.length>0){
			score += 15;
		}
	}
	return score;
}


// Companies Working on the Disease:
function read_companies(companies, biov_domain, domain_used){
    if (!companies) return;
	
    if(companies.length > 0) {
    	
		var companyLinkInfo = Object();    	
		companyLinkInfo.useLink = true;
		companyLinkInfo.relDir = 'company';
    	createListWithHeader(companies,'Companies involved:','company_header', 3, companyLinkInfo, true, 25);
	}
}

// Need to figure out how to get this to bioventurist.js
function x(i,j,t,name, topic, freebaseDomain, table, maxlength) {
	jsonp_totalblurb_freebase(freebaseDomain, name, topic, maxlength, 
		function(m){
		},
		function(m){
			t.setCell(parseFloat(i), j, m.result.body);
   			table.draw(t, {allowHtml: true});			
		},
		function(m){} 
	);
}

	
	
function read_parentalDiseases(url_read, domain_used, name){
	var q = '{"query":[{"parent_medical_condition":[{"name":null}],"name":"'+name+'","type":"'+biov_domain+'bv_medical_condition"}]}'

	perform_freebase_read(
		url_read,
		q,
		function (msg) {
			var disease = msg.result;
	        if (!disease[0]) return;
	        parentDiseases = disease[0].parent_medical_condition;
	        if(parentDiseases.length > 0) {
	      		var h = '<br></br><h3><span>Parental Disease</span></h3>';
				for (var j in parentDiseases) {
					var dis = parentDiseases[j].name;
					h += '<a href="/disease/' + dis + '">'+dis+'</a>'
					h += ( j < parentDiseases.length-1 ? ', ' :"");
				}
				h = $(h)
				$("#parentDiseases").append(h);
			}
	        },
		function(m){}
	);
}

function read_therapeutics(diseaseBlocks, url_read, domain_used, biov_domain, name){
	
	var allDiseases = '';
	var therapeutics = Array();
	for(i=0;i<1;i++){
		allDiseases = diseaseBlocks[0];
	
		var q = '{"query":[{"'+biov_domain+'bv_therapeutic/biomolecule_or_target":[],"'+biov_domain+'bv_therapeutic/indication_or_disease":[{"a:disease|=":['+allDiseases+'],"disease":[{"name":null,"id":null}],"phase_of_development":null}],"a:type":[],"brand_name":[],"a:developed_by":[{"name":null,"id":null}],"name":null,"id":null,"sales":[{"amount":null,"area":null,"optional":true,"year":null}],"stage_of_development":null,"technology_involved":[],"type":"'+biov_domain+'product"}]}';
		//alert(q);
		//var q = '{"query":[{"/user/bioventurist/default_domain/bv_therapeutic/biomolecule_or_target":[],"/user/bioventurist/default_domain/bv_therapeutic/indication_or_disease":[{"disease":"'+name+'","phase_of_development":null}],"a:type":[],"brand_name":[],"a:developed_by":[],"name":null,"sales":[{"amount":null,"area":null,"optional":true,"year":null}],"stage_of_development":null,"technology_involved":[],"type":"/user/bioventurist/default_domain/product"}]}';
		perform_freebase_read(
			url_read,
			q,
			function (msg) {
				//alert("success");
				var therapeutic = null;
				therapeutic = msg.result[0];
		        if (!therapeutic) return;		
				therapeutics = msg.result;
		        if(therapeutics.length > 0) {
		        
					// Headers
					var h_TherMarket = '<h3><span>Therapeutics on the Market</span></h3>';
					h_TherMarket = $(h_TherMarket)
					$("#h_TherMarket").append(h_TherMarket); 
					var h_TherMarket = '<h3><span>Therapeutics in Development</span></h3>';
					h_TherMarket = $(h_TherMarket)
					$("#h_TherDiv").append(h_TherMarket); 
		        
	  				var marketCols = [0,1,2,3,4];
					var devCols = [0,1,2];
					var barOptions = Object();
					barOptions.salesByCompanies = false;
					barOptions.salesByProducts = true;
					var companyOptions = Object();
					companyOptions.specifyCompany = false;
					companyOptions.companyName = '';
					createTherapeuticsTable(therapeutics, domain_used, biov_domain, marketCols, devCols, barOptions, false, companyOptions);		
				}
	        },
			function(m){}
		);
	}
}
